﻿using KouDai.Core.Service.Navigation;
using KouDai.Core.ViewModel;
using Microsoft.Phone.Controls;
using System;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace KouDai.Phone.Service
{
    public class PhoneNavigateService : SimpleNavigateService<String>
    {
        private object _parameter;

        public PhoneNavigateService()
        {
            Frame.Navigated += OnNavigated;
            Register(typeof(MainViewModel), "/MainPage.xaml");
            Register(typeof(ItemDetailViewModel), "/Views/ItemDetailPage.xaml");
        }

        public Frame Frame
        {
            get
            {
                return App.RootFrame;
            }
        }

        void OnNavigated(object sender, NavigationEventArgs e)
        {
            PhoneApplicationPage page = Frame.Content as PhoneApplicationPage;
            var dataContext = page.DataContext as NavigateViewModel;
            dataContext.Data = _parameter;
        }


        public override bool CanGoBack
        {
            get { return Frame.CanGoBack; }
        }

        protected override void NavigateTo(string view, object parameter)
        {
            _parameter = parameter;
            Frame.Navigate(new Uri(view, UriKind.Relative));
        }

        protected override void GoBackCore()
        {
            Frame.GoBack();
        }
    }
}
